package com.moli.iov.protocol.enums;

/*
 * 文件名： DateTimeTag.java
 * 
 * 创建日期： 2016年11月24日
 *
 * Copyright(C) 2016, by <a href="mailto:liws@xingyuanauto.com">liws</a>.
 *
 * 原始作者: liws
 *
 */

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * 传输协议时间定义
 *
 * @author <a href="mailto:liws@xingyuanauto.com">liws</a>
 *
 * @version $Revision$
 *
 * @since 2016年11月24日
 */
public class DateTimeTag implements Serializable {

	private static final long serialVersionUID = 1L;
	
    static final ThreadLocal<GregorianCalendar> gcc = new ThreadLocal<GregorianCalendar>() {
		@Override
        protected GregorianCalendar initialValue() {
            return new GregorianCalendar(TimeZone.getTimeZone("GMT+08:00"));
        }
    };
    
	//当前年份 前两位 整数
	private static final int currYearBefore2;
	
	static{
		String pYear = String.valueOf(gcc.get().get(Calendar.YEAR));
		pYear = pYear.substring(0,2);
		currYearBefore2 = Integer.parseInt(pYear) * 100;
	}

	public DateTimeTag() {
		this(new Date());
	}

	public DateTimeTag(Date date) {
		this((date.getTime()/1000)*1000);
	}

	public DateTimeTag(long millis) {
		gcc.get().setTimeInMillis(millis);
		this.year = (byte) ((gcc.get().get(Calendar.YEAR)-currYearBefore2)&0xff);
		this.month = (byte) ((gcc.get().get(Calendar.MONTH) + 1)&0xff);
		this.day = (byte) (gcc.get().get(Calendar.DATE)&0xff);
		this.hour = (byte) (gcc.get().get(Calendar.HOUR_OF_DAY)&0xff);
		this.minute = (byte) (gcc.get().get(Calendar.MINUTE)&0xff);
		this.second = (byte) (gcc.get().get(Calendar.SECOND)&0xff);
	}
	/**
	 * 字节数组转换为Date
	 * 
	 * @return
	 */
	public static Date toDate(byte[] bytes){
		if(bytes==null || bytes.length < 6) 
			return null;
		int year =  bytes[0] & 0xff;
		int month =  bytes[1] & 0xff;
		int day =  bytes[2] & 0xff;
		int hourOfDay =  bytes[3] & 0xff;
		int minute =  bytes[4] & 0xff;
		int second =  bytes[5] & 0xff;
		gcc.get().set(year+currYearBefore2, month-1, day, hourOfDay, minute, second);
		long millis = (gcc.get().getTimeInMillis()/1000)*1000;
		return new Date(millis);
	}
	/**
	 * 字节数组转换为对象
	 * 
	 * @return
	 */
	public static DateTimeTag valueOf(byte[] bytes){
		if(bytes == null || bytes.length < 6) 
			return null;
		int year =  bytes[0] & 0xff;
		int month =  bytes[1] & 0xff;
		int day =  bytes[2] & 0xff;
		int hourOfDay =  bytes[3] & 0xff;
		int minute =  bytes[4] & 0xff;
		int second =  bytes[5] & 0xff;
		gcc.get().set(year+currYearBefore2, month-1, day, hourOfDay, minute, second);
		long millis = (gcc.get().getTimeInMillis()/1000)*1000;
		return new DateTimeTag(millis);
	}
	private byte year;

	private byte month;

	private byte day;

	private byte hour;

	private byte minute;

	private byte second;

	public byte getYear() {
		return year;
	}

	public void setYear(byte year) {
		this.year = year;
	}

	public byte getMonth() {
		return month;
	}

	public void setMonth(byte month) {
		this.month = month;
	}

	public byte getDay() {
		return day;
	}

	public void setDay(byte day) {
		this.day = day;
	}

	public byte getHour() {
		return hour;
	}

	public void setHour(byte hour) {
		this.hour = hour;
	}

	public byte getMinute() {
		return minute;
	}

	public void setMinute(byte minute) {
		this.minute = minute;
	}

	public byte getSecond() {
		return second;
	}

	public void setSecond(byte second) {
		this.second = second;
	}
	/**
	 * 获取Date对象
	 * 
	 * @return
	 */
	public Date getDate(){
		gcc.get().set(year+currYearBefore2, month-1, day, hour, minute, second);
		long millis = (gcc.get().getTimeInMillis()/1000)*1000;
		return new Date(millis);
	}
	
	/**
	 * 获取Date毫秒级
	 * 
	 * @return
	 */
	public Long getDateLong(){
        gcc.get().set(year+currYearBefore2, month-1, day, hour, minute, second);
        long millis = (gcc.get().getTimeInMillis()/1000)*1000;
        return millis;
    }
	/**
	 * 转换为6字节数组[年,月,日,时,分,秒]
	 * 
	 * @return
	 */
	public byte[] toBytes() {
		byte[] dateBytes = new byte[6];
		dateBytes[0] = this.year;
		dateBytes[1] = this.month;
		dateBytes[2] = this.day;
		dateBytes[3] = this.hour;
		dateBytes[4] = this.minute;
		dateBytes[5] = this.second;
		return dateBytes;
	}
	@Override
	public String toString() {
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("(");
		sBuilder.append(this.getYear()+"-"+this.getMonth()+"-"+this.getDay()+" ");
		sBuilder.append(this.getHour()+":"+this.getMinute()+":"+this.getSecond());
		sBuilder.append(")");
		return sBuilder.toString();
	}
	public static void main(String[] args) {
		byte[] bytes = {(byte)16,(byte)12,(byte)02,(byte)14,(byte)07,(byte)10};
		System.out.println("222");
	}
}
